package com.qwb.weibo.Controller;

import com.qwb.weibo.DAO.UserDao;
import com.qwb.weibo.Model.User;
import com.qwb.weibo.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;



/**
 * Created by ThinkPad on 2016/12/19.
 */
@Controller
@RequestMapping("/")
public class MainController {

    @Autowired private UserService userService;

    @Autowired UserDao userDao;

    //显示登录页面
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String showLogin() {
        //返回的是View文件名

        return "general/login";
    }

    //用户登录
    @RequestMapping(value = "/",method = RequestMethod.POST)
    public String showCustomerIndex(HttpSession session,Model model,
                                    @RequestParam("userName") String userName,
                                    @RequestParam("passWord") String passWord){
       String errmsg = null;
        //根据输入的用户名和密码查询用户并创建用户对象
      User user = userService.login(userName,passWord);
      //判断用户是否存在
       if( user==null ){
            //用户不存在
           model.addAttribute("errmsg","亲，用户名或密码不存在。请检查用户名与密码重新登录");
           return "/";
        }else{
            //用户存在
           session.setAttribute("Login_User", user);
           return "customerInfor/customerIndex";
       }
    }

    //显示注册页面
    @RequestMapping(value = "/register",method = RequestMethod.GET)
     public String showRegister(){
        //返回的是view文件名

        return "general/register";
    }

    /**
     *实现用户注册功能
     */
    @RequestMapping(value = "/register",method = RequestMethod.POST)
     public String showdoRegister(HttpSession session, Model model,
                                  @RequestParam("userName") String userName,
                                  @RequestParam("userNick") String userNick,
                                  @RequestParam("userTel") String userTel,
                                  @RequestParam("userMail") String userMail,
                                  @RequestParam("userPass") String passWord,
                                  @RequestParam("region1") String region1,
                                  @RequestParam("region2") String region2,
                                  @RequestParam(value = "region3",required = false) String region3,
                                  @RequestParam("year") String year,
                                  @RequestParam("month") String month,
                                  @RequestParam("date") String date) {
        String errmsg = null;
        //创建User对象
        User user = new User(0,userName, userNick, userTel, userMail, passWord, region1, region2, region3, year, month, date);
        int a = userService.register(user, session);
        if (a == 0) {
            //添加失败
            model.addAttribute("errmsg", "添加失败，请重试！");
            return "register";
        } else {
            //添加成功
            return "general/login";
        }
    }

//        /**
//         * 显示微博主页
//         */
//        @RequestMapping(value = "customerInfor/customerIndex",method = RequestMethod.GET)
//            public String showCustomerIndex(){
//             //返回的是view文件名
//            return "customerInfor/customerIndex";
//        }

}
